package cn.gok.service.impl;

import cn.gok.dao.StudentDao;

import cn.gok.entity.TManager;
import cn.gok.entity.TStudent;
import cn.gok.service.StudentService;
import cn.gok.util.Const;
import cn.gok.util.R;
import cn.gok.util.Sqlsessionutil;
import cn.gok.util.TokenCashUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;


public class StudentServiceImpl implements StudentService {
    private SqlSession sqlSession;
    private StudentDao studentDAO;

    public StudentServiceImpl(){
        sqlSession = Sqlsessionutil.getSqlSessionFactory().openSession(true);
        studentDAO = sqlSession.getMapper(StudentDao.class);
    }

    //登录
    @Override
    public R queryStudentByNameAndPassword(String name, String password) {
        TStudent student = studentDAO.queryStudentByNameAndPassword(name, password);
        if (student != null){
            R data = R.success(Const.LOGIN_SUCCESS_MSG);
            data.put("student",student);

            return data;
        }
        else {
            TManager tManager = studentDAO.queryManagerByNameAndPassword(name, password);
            if (tManager!=null){
                R data=R.success1(Const.LOGIN_SUCCESS_MSG);
                System.out.println(data.get(4));
                data.put("manager",tManager);
                return data;
            }
            else {
                return R.fail(Const.FAIL_OPERATION_MSG);
            }

        }

    }

    //通过学生账号查询账号名是否重复
    @Override
    public R queryStudentByStudentName(String name,int id) {
        TStudent student = studentDAO.queryStudentByStudentName(name,id);
        if (student!=null){
            R success = R.success(Const.SUCCESS_OPERATION_MSG);
            return success;
        }
        return R.fail(Const.FAIL_OPERATION_MSG);
    }

    //通过学生电话查询电话是否重复
    @Override
    public R queryStudentByPhone(String phone,int id) {
        TStudent student = studentDAO.queryStudentByPhone(phone,id);
        if (student!=null){
            R success = R.success();
            return success;
        }
        return R.fail();
    }

    @Override
    public R queryStudentByStudentName1(String name) {
        TStudent student = studentDAO.queryStudentByStudentName1(name);
        if (student!=null){
           return R.success();
        }
        return R.fail();
    }

    @Override
    public R queryStudentByPhone1(String phone) {
        TStudent student = studentDAO.queryStudentByPhone1(phone);
        if (student!=null){
            return R.success();
        }
        return R.fail();
    }

    //修改学生信息
    @Override
    public R updateStudentInfo(int id, String name, String realName, int age, String gender, String phone, String school) {
        int i = studentDAO.updateStudentInfo(id, name, realName, age, gender, phone, school);
        if (i!=0){
            R success = R.success();
            return success;
        }
        return R.fail();
    }

    //查询密码是否正确
    @Override
    public R queryPasswordByPassword(String password, int id) {
        TStudent student = studentDAO.queryPasswordByPassword(password, id);
        if (student!=null){
            R success = R.success();
            return success;
        }
        return R.fail();

    }

    //修改密码
    @Override
    public R updatePasswordByPassword(String password, int id) {
        int i = studentDAO.updatePasswordByPassword(password, id);
        if (i>0){
            R success = R.success();
            return success;
        }
        return R.fail();
    }

    @Override
    public R checkStudent(String stuName, String question, String answer) {
        int i = studentDAO.queryStudentInfoByStudentNameAndQuestion(stuName, question, answer);
        if (i>0){
            //成功
                //将token响应给结果集
            String tokenValue = UUID.randomUUID().toString();

            TokenCashUtil.putToken(TokenCashUtil.TOKEN_+stuName,tokenValue);
            R r = R.success("密保验证成功");
            r.put("token",tokenValue);
            return r;


        }
        else {
            //失败
        }
        return R.fail();
    }

    @Override
    public R addSecurityQuestionAndAnswerInfo(int stuId, String question, String answer) {
        int i = studentDAO.addSecurityQuestionAndAnswerInfo(stuId, question, answer);
        if (i>0){
            //添加成功
            R success = R.success();
            return success;
        }
        else {
            return R.fail();
        }
    }

    //通过账号查找密保
    @Override
    public R getQuestionById(String stuId) {
        List<String> questionById = studentDAO.getQuestionById(stuId);
        return R.create(questionById);

    }

    @Override
    public R checkQuestionAndAnswer(String question, String answer, String studentName) {
        TStudent student = studentDAO.checkQuestionAndAnswer(question, answer, studentName);
        if (student!=null){
          return   R.success();
        }
        else {
            return R.fail();
        }
    }

}
